import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '@/views/HomeView/HomeView.vue'
import Layout from "@/views/Layout.vue";
import {useLoginStore} from "@/stores/loginStor.js";
import {useMenuStore} from "@/stores/menuStore.js";
const routes = [
    {
        path: '/',
        name: 'layout',
        component: Layout,
        meta:{
            requireAuth: true
        },
        children: [
            {
                path: '/',
                name: 'home',
                component: HomeView,
                meta:{
                    requireAuth: true,
                    key:'首页'
                },
            },
            {
                path: '/project',
                name: 'project',
                component: () => import('@/views/ProjectInfo/projectInfo.vue'),
                meta:{
                    requireAuth: true,
                    key:'项目基础信息'
                }
            },
            {
                path: '/tunnel',
                name: 'tunnel',
                component: () => import('@/views/TunnelInfo/TunnelInfo.vue'),
                meta:{
                    requireAuth: true,
                    key:'隧道设计信息'
                }
            },
            {
                path: '/build',
                name: 'build',
                component: () => import('@/views/BuildMange/BuildMange.vue'),
                meta:{
                    requireAuth: true,
                    key:'施工监控监测'
                }
            },
            {
                path: '/geological',
                name: 'geological',
                component: () => import('@/views/GeologicalInfo/GeologicalInfo.vue'),
                meta:{
                    requireAuth: true,
                    key:'超前地址预报'
                }
            },
            {
                path: '/system',
                name: 'system',
                component: () => import('@/views/SystemMange/SystemMange.vue'),
                meta:{
                    requireAuth: true,
                    key:'系统信息管理'
                }
            },
            {
                path: '/userCenter',
                name: 'userCenter',
                component: () => import('@/views/UserCenter/UserCenter.vue'),
                meta:{
                    requireAuth: true,
                    key:'个人中心'
                }
            },
            {
                path:'/plan',
                name:'检测计划',
                component: () => import('@/views/BuildMange/PlanTest.vue'),
                meta:{
                    requireAuth: true,
                    key:'检测计划'
                }
            },
            {
                path:'section',
                name:'切面检测',
                component: () => import('@/views/BuildMange/SectionTest.vue'),
                meta:{
                    requireAuth: true,
                    key:'切面检测'
                }
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/LoginInfo/LoginInfo.vue')
    },
    {
        path:'/pdf',
        name:'pdf',
        component: () => import('@/views/TunnelInfo/PDFViewer/PDFViewer.vue')
    },
    {
        path:'/:pathMatch(.*)*',
        name:'notFound',
        component: () => import('@/views/NotFound/NotFound.vue'),
    }

]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
     const loginStore = useLoginStore()
    if(to.meta.requireAuth){
        if(!loginStore.token){
            next({
              path: '/login'
            })
        }else {
            next()
        }
    }else {
        next()
    }
})
router.afterEach((to, from) => {
    //存储路由路径
    localStorage.setItem("active", to.path)
    if(to.meta.key) {
        const menuStore = useMenuStore()
        menuStore.breadcrumb= to.meta.key
    }
})

export default router 